package com.laowang.common.utils.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.laowang.common.utils.DateUtil;
import com.laowang.common.utils.DateUtils;
import com.laowang.common.utils.security.Md5Utils;
import com.laowang.project.system.domain.vo.*;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class HttpKit {

	private static final String DEFAULT_CHARSET = "UTF-8";

	/**
	 * @return 返回类型:
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 * @throws NoSuchProviderException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @description 功能描述: get 请求
	 */
	public static String get(String url, Map<String, String> params, Map<String, String> headers) throws IOException, ExecutionException, InterruptedException {
		AsyncHttpClient http = new AsyncHttpClient();
		AsyncHttpClient.BoundRequestBuilder builder = http.prepareGet(url);
		builder.setBodyEncoding(DEFAULT_CHARSET);
		if (params != null && !params.isEmpty()) {
			Set<String> keys = params.keySet();
			for (String key : keys) {
				builder.addQueryParam(key, params.get(key));
			}
		}

		if (headers != null && !headers.isEmpty()) {
			Set<String> keys = headers.keySet();
			for (String key : keys) {
				builder.addHeader(key, headers.get(key));
			}
		}
		Future<Response> f = builder.execute();
		String body = f.get().getResponseBody(DEFAULT_CHARSET);
		http.close();
		return body;
	}

	/**
	 * @return 返回类型:
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 * @throws NoSuchProviderException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @description 功能描述: get 请求
	 */
	public static String get(String url) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, UnsupportedEncodingException, IOException, ExecutionException,
			InterruptedException {
		return get(url, null);
	}

	/**
	 * @return 返回类型:
	 * @throws IOException
	 * @throws NoSuchProviderException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @throws UnsupportedEncodingException
	 * @description 功能描述: get 请求
	 */
	public static String get(String url, Map<String, String> params) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, UnsupportedEncodingException, IOException,
			ExecutionException, InterruptedException {
		return get(url, params, null);
	}

	/**
	 * @return 返回类型:
	 * @throws IOException
	 * @throws NoSuchProviderException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @description 功能描述: POST 请求
	 */
	public static String post(String url, Map<String, String> params) throws IOException, ExecutionException, InterruptedException {
		AsyncHttpClient http = new AsyncHttpClient();
		AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
		builder.setBodyEncoding(DEFAULT_CHARSET);
		if (params != null && !params.isEmpty()) {
			Set<String> keys = params.keySet();
			for (String key : keys) {
				builder.addQueryParam(key, params.get(key));
			}
		}
		Future<Response> f = builder.execute();
		String body = f.get().getResponseBody(DEFAULT_CHARSET);
		http.close();
		return body;
	}
	
	public static String post(String url, Map<String, String> params ,Map<String, String> headers) throws IOException, ExecutionException, InterruptedException {
		AsyncHttpClient http = new AsyncHttpClient();
		AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
		builder.setBodyEncoding(DEFAULT_CHARSET);
		if(headers != null && !headers.isEmpty()){
			Set<String> keys = headers.keySet();
			for (String key : keys) {
				builder.addHeader(key, headers.get(key));
			}
		}
		if (params != null && !params.isEmpty()) {
			Set<String> keys = params.keySet();
			for (String key : keys) {
				builder.addQueryParam(key, params.get(key));
			}
		}
		Future<Response> f = builder.execute();
		String body = f.get().getResponseBody(DEFAULT_CHARSET);
		http.close();
		return body;
	}
	
	public static String post(String url, String s ,Map<String, String> headers) throws IOException, ExecutionException, InterruptedException {
		AsyncHttpClient http = new AsyncHttpClient();
		AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
		builder.setBodyEncoding(DEFAULT_CHARSET);
		if(headers != null && !headers.isEmpty()){
			Set<String> keys = headers.keySet();
			for (String key : keys) {
				builder.addHeader(key, headers.get(key));
			}
		}
		builder.setBody(s);
		Future<Response> f = builder.execute();
		String body = f.get().getResponseBody(DEFAULT_CHARSET);
		http.close();
		return body;
	}

	public static String post(String url, String s) throws IOException, ExecutionException, InterruptedException {
		AsyncHttpClient http = new AsyncHttpClient();
		AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
		builder.setBodyEncoding(DEFAULT_CHARSET);
		builder.setBody(s);
		Future<Response> f = builder.execute();
		String body = f.get().getResponseBody(DEFAULT_CHARSET);
		http.close();
		return body;
	}

	public static byte[] postAsByte(String url, String s) throws IOException, ExecutionException, InterruptedException {
		AsyncHttpClient http = new AsyncHttpClient();
		AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
		builder.setBodyEncoding(DEFAULT_CHARSET);
		builder.setBody(s);
		Future<Response> f = builder.execute();
		byte[] body = f.get().getResponseBodyAsBytes();
		http.close();
		return body;
	}

	
	public static void main(String[] args) {
        Gson gson = new Gson();
		String url ="http://ztopenapi.crland.com.cn/api-gateway/rs-service/";
		String Api_ID="mixc.imPOSWBJB.GLWXCJB.orderCollect";
		String Api_Version="1.0.1";
		String cashierId ="1102a001l0208n0501"; //收银员编号
		String checkCode ="p88888888";
		String store ="L0208N05"; //店铺编号
		String mall ="1102A001"; //商场编号
		String tillId ="01"; //收银机编号
		String App_Pub_ID ="10000187223RL";
		String Partner_ID ="70000029";
		String App_ID ="10000187223IS";  //App_ID即App_Sub_ID
		String signPass ="3c67b01b20a8427d8cb138d7f0e41eea";
		String Token ="630bba0e63f746bd87c016b7796358a0";
        String Sys_ID ="100001872";
        String Sign_Method="md5" ;
        String type ="SALE";
        String orderId = "0961001100120041010007";

        PayList payList = new PayList();
        payList.setPayAmt(100);
        payList.setValue(100);
        payList.setDiscountAmt(0);
        payList.setPaymentMethod("CH");

        String time = DateUtils.dateTimeNow();
        payList.setTime(time);

        String time1 = DateUtils.getTime1();
        List<PayList> payLists = new ArrayList<>();
        payLists.add(payList);

        List<ItemList> itemLists =new ArrayList<>();

        RequestData requestDataObject = new RequestData();

        requestDataObject.setPayList(payLists);
        requestDataObject.setItemList(itemLists);
        requestDataObject.setCashierId(cashierId); //收银员编号
        requestDataObject.setCheckCode(checkCode);
        //requestDataObject.setComments("test");
        requestDataObject.setMall(mall);//商场编号
        requestDataObject.setOrderId(orderId);
        requestDataObject.setStore(store);//
        requestDataObject.setTillId(tillId); // 收银机编号
        requestDataObject.setTime(time);
        requestDataObject.setTotalAmt(100);
        requestDataObject.setType(type);

        //String requestData = JSON.toJSONString(requestDataObject);
        String requestData = gson.toJson(requestDataObject);
        //{"cashierId":"0202a501bhsl113n0101","checkCode":"88888888","comments":"test","mall":"0202A501","mobile":"","orderId":"HSS0351001100120041010007","store":"BHSL113N01","tillId":"01","time":"20211214095527","totalAmt":100,"type":"SALE","itemList":[{"itemCode":"BHSL113N0100","price":100,"quantity":1}],"payList":[{"cardBank":"","cardNumber":"","discountAmt":0,"payAmt":100,"paymentMethod":"CH","time":"20211214095526","value":100}]}
		String sign  ="Api_ID="+Api_ID+ "&Api_Version=" +Api_Version+ "&App_Pub_ID="+App_Pub_ID + "&App_Sub_ID="+App_Pub_ID + "&App_Token="+Token+ "&Format=json"+ "&Partner_ID="+Partner_ID+ "&REQUEST_DATA="+requestData+ "&Sign_Method="+Sign_Method+ "&Sys_ID="+Sys_ID+  "&Time_Stamp="+time1+"&"+signPass;
        sign = Md5Utils.hash(sign).toUpperCase();

        RequestObject2 requestObject2 = new RequestObject2();
        requestObject2.setApi_ID(Api_ID);
        requestObject2.setApi_Version(Api_Version);
        requestObject2.setApp_Pub_ID(App_Pub_ID);
        requestObject2.setApp_Token(Token);
        requestObject2.setPartner_ID(Partner_ID);
        requestObject2.setApp_Sub_ID(App_ID);
        requestObject2.setFormat("json");
        requestObject2.setTime_Stamp(time1);
        requestObject2.setSign_Method(Sign_Method);
        requestObject2.setSign(sign);
        requestObject2.setSys_ID(Sys_ID);

        RequestObject1 requestObject1 = new RequestObject1();
        requestObject1.setHRT_ATTRS(requestObject2);

        RequestData requestData1 = new RequestData();
        requestData1.setType(type);
        requestData1.setTotalAmt(100);
        requestData1.setTime(time);
        requestData1.setTillId(tillId);
        requestData1.setStore(store);
        requestData1.setOrderId(orderId);
        requestData1.setMall(mall);
        requestData1.setCheckCode(checkCode);
        requestData1.setCashierId(cashierId);
        requestData1.setItemList(itemLists);
        requestData1.setPayList(payLists);

        requestObject1.setREQUEST_DATA(requestData1);

        RequestObject requestObject = new RequestObject();
        requestObject.setREQUEST(requestObject1);

        //String str = JSON.toJSONString(requestObject);
        //String str = JSONObject.toJSONString(requestObject);
        String str = gson.toJson(requestObject);
        try {
            Map<String,String> headers  =new HashMap<>();
            headers.put("Content-Type","application/json;charset=UTF-8");
            String post = post(url, str,headers);
            System.out.println(post);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
